package site.lianwu.mybatis.plus.query.processor;

import site.lianwu.mybatis.plus.query.model.ExecutableQueryStatement;
import site.lianwu.mybatis.plus.query.model.QueryClass;

import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import java.util.function.Function;

/**
 * 查询类处理器的装饰器
 * @author LianWu
 * @since 2.0.0
 */
public abstract class QueryClassProcessorDecorator implements QueryClassProcessor {

	protected final QueryClassProcessor queryClassProcessor;

	public QueryClassProcessorDecorator() {
		this(GeneralQueryClassProcessor::new);
	}

	public QueryClassProcessorDecorator(Collection<QueryFieldProcessor> queryFieldProcessors) {
		this.queryClassProcessor = new GeneralQueryClassProcessor(queryFieldProcessors);
	}

	public QueryClassProcessorDecorator(Function<Collection<QueryFieldProcessor>, QueryClassProcessor> queryClassProcessorProvider) {
		List<QueryFieldProcessor> fieldProcessors = new ArrayList<>(2);
		fieldProcessors.add(new GeneralQueryFieldProcessor());
		fieldProcessors.add(new NestedQueryFieldProcessor(this));
		this.queryClassProcessor = queryClassProcessorProvider.apply(fieldProcessors);
	}

	public QueryClassProcessorDecorator(Collection<QueryFieldProcessor> queryFieldProcessors, Function<Collection<QueryFieldProcessor>, QueryClassProcessor> queryClassProcessorProvider) {
		this.queryClassProcessor = queryClassProcessorProvider.apply(queryFieldProcessors);
	}

	public QueryClassProcessorDecorator(QueryClassProcessor queryClassProcessor) {
		this.queryClassProcessor = queryClassProcessor;
	}

	@Override
	public QueryClass extract(Class<?> clz, boolean includeInheritedFields) {
		return queryClassProcessor.extract(clz, includeInheritedFields);
	}

	@Override
	public List<ExecutableQueryStatement> parse(QueryClass queryClass, Object obj) {
		return queryClassProcessor.parse(queryClass, obj);
	}

}
